package com.zxh.okretrofit.interceptor;

import java.io.IOException;

import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 登录同步拦截器
 * Created by zhangxuehui on 2018/2/8.
 */
public class AuthInterceptor implements Interceptor {
    private String token, version;

    public AuthInterceptor(String token, String version) {
        this.token = token;
        this.version = version;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request originalRequest = chain.request();

        HttpUrl newUrl = originalRequest.url().newBuilder()
                .scheme(originalRequest.url().scheme())
                .host(originalRequest.url().host())
                .addQueryParameter("authSign", token)
                .addQueryParameter("app_version", version)
                .build();

        Request newRequest = originalRequest.newBuilder()
                .addHeader("User-Agent", "android")
                .method(originalRequest.method(), originalRequest.body())
                .url(newUrl)
                .build();
        return chain.proceed(newRequest);
    }

}